package com.java.module.user.controller.api;

import com.java.common.lang.exception.BaseException;
import com.java.common.utils.StringUtils;
import com.java.common.utils.VipConfig;
import com.java.module.user.domain.Search;
import com.java.module.user.domain.dto.*;
import com.java.module.user.domain.vo.UserVip;
import com.java.module.user.service.AliyunMobileAuthService;
import com.java.module.user.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.java.common.lang.annotation.ValidateToken;
import com.java.common.lang.domain.entity.User;
import com.java.common.service.ApiTokenService;
import com.java.module.user.domain.vo.UserLoginVO;
import com.java.module.user.service.IFeedbackService;
import com.java.module.user.service.IUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

/**
 * 用户信息Controller
 * 
 * @author Administrator
 * @date 2021-02-02
 */
@ApiSort(100)
@Api(tags = "用户管理")
@RestController("ApiUserController")
@RequestMapping("/user")
public class UserController {

    @Autowired
    ApiTokenService tokenService;

    @Autowired
    IUserService userService;

    @Autowired
    IFeedbackService feedbackService;

    @Autowired
    AliyunMobileAuthService authService;
    @Autowired
    private ApiTokenService apiTokenService;

    @Autowired
    private ISearchService searchService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("用戶登录")
    @ValidateToken(isValidate = false)
    @PostMapping("/login")
    public UserLoginVO login(@Validated @RequestBody UserLoginDTO user) {
        return userService.login(user);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("个人信息")
    @GetMapping("/getUserInfo")
    public User getUserInfo() {
        return userService.getUserInfo();
    }



    @ApiOperationSupport(order = 2)
    @ApiOperation("直接查询个人信息")
    @GetMapping("/getUserBaseInfo/{uid}")
    public User getUserBaseInfo(@PathVariable("uid") Long uid) {
        return userService.getUserBaseInfo(uid);
    }


    @ValidateToken(isValidate = false)
    @ApiOperationSupport(order = 2)
    @ApiOperation("查询置顶人员数据")
    @GetMapping("/getTopList")
    public List<User> getTopList() {
        User user = new User();
        user.setIsTop(1);
        return userService.selectUserList(user);
    }

    @ApiOperationSupport(order = 21)
    @ApiOperation("随机人员")
    @GetMapping("/randomUser")
    public UserVip randomUser(User user) {

        //在这里判断剩余次数

        User user1 = userService .selectUserById(apiTokenService.getLoginUserId());

        Search search = new Search();
        search.setSelectUid(user1.getUid());
        List<Search>    searchList = searchService.selectSearchList(search);
        int maxSelect = 2;
        if(user1.getVip() == 1){
            maxSelect = VipConfig.matchDays;
        }
        if(searchList.size() > 0 && searchList.size()>= maxSelect) {
            Assert.isTrue(false,"今日已超出"+maxSelect+"匹配次数");
        }

        UserVip userVip = new UserVip();
        user.setUid(tokenService.getLoginUserId());
        userVip.setUserList(userService.selectUserListRandom(user));
        userVip.setRemainingNUmber(maxSelect-1-searchList.size());
        searchService.insertSearch(search);

        return userVip;
    }



    @ApiOperationSupport(order = 21)
    @ApiOperation("每日是否有匹配次数")
    @GetMapping("/overNumber")
    public int overNumber() {
        //在这里判断剩余次数
        User user1 = userService .selectUserById(apiTokenService.getLoginUserId());
        Search search = new Search();
        search.setSelectUid(user1.getUid());
        List<Search>    searchList = searchService.selectSearchList(search);
        int maxSelect = 2;
        if(user1.getVip() == 1){
            maxSelect = VipConfig.matchDays;
        }
        if(searchList.size() > 0 && searchList.size()>= maxSelect) {
            Assert.isTrue(false,"今日已超出"+maxSelect+"匹配次数");
        }
        return maxSelect-searchList.size();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("更新个人信息")
    @PostMapping("/updateUserInfo")
    public int updateUserInfo(@Validated @RequestBody UserInfoDTO userInfoDTO) {
        return userService.updateUserInfo(userInfoDTO);
    }


    @ApiOperationSupport(order = 3)
    @ApiOperation("更换手机号")
    @PostMapping("/changePhone")
    public int changePhone(@Validated @RequestBody ChangePhoneDto changePhoneDto) {
        return userService.updateChangePhone(changePhoneDto);
    }



    @ApiOperationSupport(order = 3)
    @ApiOperation("提交认证")
    @PostMapping("/submitPersonStatus")
    public int submitPersonStatus(@Validated @RequestBody UserInfoDTO userInfoDTO) {
        userInfoDTO.setCheckStatus("1");
        return userService.updateUserInfo(userInfoDTO);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("发送短信")
    @PostMapping("/sendSms")
    @ValidateToken(isValidate = false)
    public int sendSms(@Validated @RequestBody SendSmsDTO sms) {
        return userService.sendSms(sms);
    }




    @ApiOperationSupport(order = 4)
    @ApiOperation("发送登录验证码短信")
    @PostMapping("/sendSmsCode")
    @ValidateToken(isValidate = false)
    public int sendSmsCode(@Validated @RequestBody SendSmsDTO sms) {
        return userService.sendSmsCode(sms);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation("用户注册")
    @PostMapping("/register")
    @ValidateToken(isValidate = false)
    public int register(@Validated @RequestBody RegisterUserDTO registerUser) {
        return userService.register(registerUser);
    }


    @ApiOperationSupport(order = 5)
    @ApiOperation("用户验证码注册")
    @PostMapping("/registerCode")
    @ValidateToken(isValidate = false)
    public UserLoginVO registerCode(@Validated @RequestBody RegisterUserDTO registerUser) {
        return userService.registerCode(registerUser);
    }
    /**
     * 忘记密码
     */
    @ApiOperationSupport(order = 6)
    @ApiOperation("重置登录密码")
    @ValidateToken(isValidate = false)
    @PostMapping("/resetLoginPassword")
    public int resetLoginPassword(@Validated @RequestBody ForgetUserDTO forgetUser) {
        return userService.resetLoginPassword(forgetUser);
    }

    /**
     * 修改登录密码
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation("修改登录密码")
    @PostMapping("/changeLoginPassword")
    public int changeLoginPassword(@Validated @RequestBody ChangeLoginPwdDTO changeLoginPwd) {
        return userService.changeLoginPassword(changeLoginPwd);
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public boolean logout() {
        return tokenService.delLoginUser();
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation("注销账号")
    @PostMapping("/cancelAccount")
    public int cancelAccount() {
        return userService.cancelAccount();
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation("意见反馈")
    @PostMapping("/addFeedback")
    public int addFeedback(@Validated @RequestBody FeedbackDTO feedbackDTO) {
        return feedbackService.addFeedback(feedbackDTO);
    }





    /**
     * 一键登录接口
     * @param
     */
    @ValidateToken(isValidate = false)
    @PostMapping("/one-click-login")
    public UserLoginVO oneClickLogin(@Validated @RequestBody PhoneDto phoneDto) {
        String accessToken = phoneDto.getToken();
        if (StringUtils.isEmpty(accessToken)) {
            throw new BaseException("当前认证信息不正确");
        }
        System.out.println(accessToken);
        String phone = authService.verifyAndLogin(accessToken);
        System.out.println(phone);
        RegisterUserDTO registerUser = new RegisterUserDTO();
        registerUser.setUsername(phone);
        registerUser.setPassword("123456");
        return userService.registerCode(registerUser);
    }







}